layui.use(['form', 'table','layuimini','element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoBookDirectory = {
        id: 'BoBookDirectoryTable'
    };

    // 当前弹出层，防止ID被覆盖
    var parentIndex = layer.index;

    var column = [
        [
            {type: 'radio'},
            {field: 'directoryName', title: '目录名', align: 'center'},
            {field: 'bookName', title: '书名', align: 'center'},
            {field: 'content', title: '简介', align: 'center',
                templet: function (object) {
                    if(object.content == '' || object.content == undefined || object.content == null){
                        return '-';
                    }else{
                        return object.content;
                    }
                }
            },
            {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
            {field: 'createDate', title: '创建时间', minWidth: 120, align: 'center'}
        ]
    ];

    table.render({
        elem: '#' + BoBookDirectory.id,
        url: '/web/boBookDirectory/list',
        toolbar: '#in_toolbarBtn',
        defaultToolbar: [],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    // 监听搜索操作
    $("#in_data-search-btn").on('click', function () {

        //获取搜索条件
        var bookName = $("#in_search_bookName").val();
        var directoryName = $("#in_search_directoryName").val();

        //封装
        var dataObject = {};
        dataObject['bookName'] = bookName;
        dataObject['directoryName'] = directoryName;
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoBookDirectory.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听重置操作
    $("#in_data-reset-btn").on('click', function () {

        //初始化
        $("#in_search_bookName").val("");
        $("#in_search_directoryName").val("");

        form.render();

        //封装
        var dataObject = {};
        dataObject['bookName'] = "";
        dataObject['directoryName'] = "";
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoBookDirectory.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });


    // 监听确定选择操作
    $(".layuimini-container").on("click", "#in_choose-btn", function () {
        var bookDirectoryId = $("#in_bookDirectoryId").val();
        var bookDirectoryName = $("#in_bookDirectoryName").val();
        $("#bookDirectoryId").val(bookDirectoryId);
        $("#bookDirectoryName").val(bookDirectoryName);
        // 关闭弹出层
        layer.close(parentIndex);
        return true;
    });


    //监听表格单选按钮
    table.on('radio(BoBookDirectoryTable)', function (obj) {
        $("#in_bookDirectoryId").val(obj.data.id);
        $("#in_bookDirectoryName").val(obj.data.directoryName);
    });

});